package controller.view;

import java.util.HashSet;
import java.util.Set;

import view.interfaces.statechart.StatechartView;
import view.interfaces.workspace.WorkspaceView;
import model.Statechart;
import model.Workspace;
import command.Command;

public class WorkspaceController {

	private final Workspace workspace;
	private final ProjectController projectController;
	private final WorkspaceView workspaceView;
	private Set<Long> lockedStatechartIds;
	
	public WorkspaceController(Workspace workspace,
			ProjectController projectController, WorkspaceView workspaceView) {
		super();
		this.workspace = workspace;
		this.projectController = projectController;
		this.workspaceView = workspaceView;
		this.lockedStatechartIds = new HashSet<Long>();
	}

	public void executeCommand(Command command, Statechart statechart) {
		if (!isLocked(statechart)) {
			projectController.executeCommand(command);
		}
	}

	public void lock(Statechart statechart) {
		lockedStatechartIds.add(statechart.getId());
	}
	
	public void unlock(Statechart statechart) {
		lockedStatechartIds.remove(statechart.getId());
	}
	
	private boolean isLocked(Statechart statechart) {
		return lockedStatechartIds.contains(statechart.getId());
	}

	public Workspace getWorkspace() {
		return workspace;
	}

	public Set<Long> getLockedStatechartIds() {
		return lockedStatechartIds;
	}

	public StatechartController createStatechartController(
			Statechart statechart, StatechartView statechartView) {
		return new StatechartController(statechart, this, statechartView);
	}

}
